function [tval, prob, df] = t_test(x,y)
%t_test - t test between two populations, assuming equal variance
% from Press et al., p. 616
% uses betai function
% Coded P. Manis, 12/2/98

tval = 0;
prob = 0;
df = 0;
if nargin ~= 2, 
    error('Requires two input arguments'); 
end
% get rid of NaN's in input.
xn = find(~isnan(x));
yn = find(~isnan(y));
ok = intersect(xn, yn); % match where BOTH are not NaN
x=x(ok);
y=y(ok);

[m1 n1] = size(x);
[m2 n2] = size(y);
if (m1 ~= 1 & n1 ~= 1) | (m2 ~= 1 & n2 ~= 1)
    error('Requires vector first and second inputs.');
end
 
n1 = length(x);
n2 = length(y);

[mean_x, var_x] = mean_var(x);
[mean_y, var_y] = mean_var(y);
df = n1 + n2 - 2;

svar = ((n1-1)*var_x + (n2-1)*var_y)/df;
tval = (mean_x-mean_y)/sqrt(svar*(1.0/n1 + 1.0/n2));
prob = beta_i(0.5*df, 0.5, df/(df+tval*tval));

return
